//
//  JXReplyMineChildCtl.m
//  JiaoX
//
//  Created by yinqy on 15/2/27.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXReplyMineChildCtl.h"
#import "ForumDataTool.h"
#import "ReplyMineCell.h"
#import "MJRefresh.h"
#import "ReplyMineCellFrame.h"
#import "JXComtDetailCtl.h"

@interface JXReplyMineChildCtl ()
@property (nonatomic, copy) NSString *replyType;
@property (nonatomic, strong)NSMutableArray *replyMineFrameArray;
@end

@implementation JXReplyMineChildCtl

+(instancetype)getInstanceWithReplyType:(NSString *)replyType{
    JXReplyMineChildCtl *ctrl = [[JXReplyMineChildCtl alloc]init];
    ctrl.replyType = replyType;
    return ctrl;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.replyMineFrameArray = [NSMutableArray array];
    [self.tableView setTableFooterView:[[UIView alloc]init]];
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    
    [self.tableView headerBeginRefreshing];
}

-(void)headerRefresh{
    [self requestDataWithLastTime:@""];
}

-(void)footerRefresh{
    if (self.replyMineFrameArray.count>0) {
        ReplyMineCellFrame *frame = [self.replyMineFrameArray lastObject];
        [self requestDataWithLastTime:frame.mineInteract.createTime];
    }else{
        [self requestDataWithLastTime:@""];
    }
}

-(void)refreshComplete{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
}

-(void)requestDataWithLastTime:(NSString *)lastTime{
    [ForumDataTool getMineInteractDataWithUrl:[NSString stringWithFormat:@"%@forum/replymine",BASE_URL] lastTime:lastTime replyType:self.replyType success:^(MineInteractResult *result) {
        [self refreshComplete];
        if ([Utils isBlankString:result.data]) {
            [self convertDataToFrame:result lastTime:lastTime];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [self refreshComplete];
        [DialogUtils showTipsWithView:self.navigationController.view String:@"加载失败"];
    }];
}

-(void)convertDataToFrame:(MineInteractResult *)result lastTime:(NSString *)lastTime{
    if ([Utils isBlankString:lastTime]) {
        [self.replyMineFrameArray removeAllObjects];
    }
    
    for (int i = 0; i<result.interactList.count; i++) {
        ReplyMineCellFrame *frame = [[ReplyMineCellFrame alloc]initWithReplyType:self.replyType];
        [frame setMineInteract:result.interactList[i]];
        [self.replyMineFrameArray addObject:frame];
    }
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.replyMineFrameArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ReplyMineCell *cell = [ReplyMineCell cellWithTableView:tableView];
    [cell setReplyMineCellFrame:self.replyMineFrameArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReplyMineCellFrame *frame = self.replyMineFrameArray[indexPath.row];
    return frame.frame.size.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReplyMineCellFrame *frame = self.replyMineFrameArray[indexPath.row];
    CommentInfo *comtInfo = frame.mineInteract.mainComment;
    JXComtDetailCtl *ctl = [[JXComtDetailCtl alloc]initWithComtId:comtInfo.commentId ReplyType:REPLY_TYPE_POSTSID replyId:frame.mineInteract.postsId startFrom:0 mainComtInfo:comtInfo];
    [[self.view.superview viewController].navigationController pushViewController:ctl animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(void)viewDidLayoutSubviews
{
    [Utils setSeparatorInsetWithTableView:self.tableView insets:UIEdgeInsetsMake(0,JXPublicInset,0,0)];
}


@end
